package com.example.szys.controler;

import com.example.szys.POJO.ClassItem;
import com.example.szys.POJO.ClassMember;
import com.example.szys.inviteCode.CreateCode;
import com.example.szys.model.Classes;
import com.example.szys.POJO.HttpResult;
import com.example.szys.model.User;
import com.example.szys.model.UserClass;
import com.example.szys.repository.ClassesRepository;
import com.example.szys.repository.UserClassRepository;
import com.example.szys.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
public class ClassesControler {
    @Autowired
    private UserClassRepository userClassRepository;
    @Autowired
    private ClassesRepository classesRepository;

    @Autowired
    private UserRepository userRepository;

    @PostMapping("/getclass")
    public HttpResult<ArrayList<ClassItem>> classList(@RequestParam("email") String email){
        HttpResult<ArrayList<ClassItem>> listHttpResult=new HttpResult<>();
        List<UserClass> userClassList=userClassRepository.findByEmail(email);
        ArrayList<ClassItem> list=new ArrayList<>();
        if(userClassList!=null) {
            for (UserClass userClass : userClassList) {
                Classes classes = classesRepository.findOne(userClass.getClassid());
                ClassItem classItem = new ClassItem();
                classItem.setClassid(userClass.getClassid());
                classItem.setClassname(classes.getName());
                classItem.setRole(userClass.isType());
                classItem.setTeacher(classes.getTeacher());
                list.add(classItem);
            }
        }
        listHttpResult.setContent(list);
        return listHttpResult;
    }

    @PostMapping("/createclass")
    public HttpResult<String> createClass(@RequestParam("email") String email,@RequestParam("classname") String classname,@RequestParam("teacher") String teacher){
        HttpResult<String> stringHttpResult=new HttpResult<>();
        String code=CreateCode.createCode();
        while (classesRepository.findOne(code)!=null){
            code=CreateCode.createCode();
        }
        Classes classes=new Classes();
        classes.setClassid(code);
        classes.setName(classname);
        classes.setTeacher(teacher);
        stringHttpResult.setContent(code);
        stringHttpResult.setResult(0);
        classesRepository.save(classes);
        UserClass userClass=new UserClass();
        userClass.setClassid(code);
        userClass.setEmail(email);
        userClass.setType(true);
        userClassRepository.save(userClass);
        return stringHttpResult;
    }

    @PostMapping(value = "/joinclass")
    public HttpResult<String> joinClass(@RequestParam("email") String email,@RequestParam("classid") String classid){
        HttpResult<String> stringHttpResult=new HttpResult<>();
        if(classesRepository.findOne(classid)==null){
            stringHttpResult.setResult(1);
            return stringHttpResult;
        }else if(userClassRepository.findByEmailAndClassid(email,classid)!=null){
            stringHttpResult.setResult(2);
            return stringHttpResult;
        }else {
            UserClass userClass=new UserClass();
            userClass.setEmail(email);
            userClass.setClassid(classid);
            userClass.setType(false);
            userClassRepository.save(userClass);
            stringHttpResult.setResult(0);
            return stringHttpResult;
        }
    }

    @PostMapping("/classmember")
    public HttpResult<ArrayList<ClassMember>> classmember(@RequestParam("classid") String classid){
        HttpResult<ArrayList<ClassMember>> arrayListHttpResult=new HttpResult<>();
        List<UserClass> userClasses=userClassRepository.findByClassid(classid);
        ArrayList<ClassMember> classMembers=new ArrayList<>();
        for(UserClass userClass : userClasses){
            User user=userRepository.findOne(userClass.getEmail());
            ClassMember classMember=new ClassMember();
            classMember.setName(user.getName());
            classMember.setRole(userClass.isType());
            classMembers.add(classMember);
        }
        arrayListHttpResult.setContent(classMembers);
        return arrayListHttpResult;
    }
}
